package com.hao.study.controller;

import com.github.pagehelper.PageInfo;
import com.hao.study.common.APIResponse;
import com.hao.study.common.ResponeCode;
import com.hao.study.entity.ErrorLog;
import com.hao.study.service.ErrorLogService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * @author: author
 * @description:    错误日志
 * @date: 2024/6/13 9:55
 * @version: 1.0
 */
@Api(tags={"错误日志管理接口"})
@RestController
@RequestMapping("/errorLog")
public class ErrorLogController {

    @Autowired
    ErrorLogService service;

    @GetMapping("/list")
    public APIResponse list(@RequestParam(defaultValue = "1")Integer pageNum,
                            @RequestParam(defaultValue = "10")Integer pageSize,
                            String keyword){
        PageInfo<ErrorLog> list =  service.getErrorLogList(pageNum,pageSize,keyword);
        return new APIResponse(ResponeCode.SUCCESS, "查询成功", list);
    }

    @PostMapping("/create")
    @Transactional
    public APIResponse create(ErrorLog errorLog) throws IOException {
        service.createErrorLog(errorLog);
        return new APIResponse(ResponeCode.SUCCESS, "创建成功");
    }

    @PostMapping("/delete")
    public APIResponse delete(String ids){
        System.out.println("ids===" + ids);
        // 批量删除
        String[] arr = ids.split(",");
        for (String id : arr) {
            service.deleteErrorLog(id);
        }
        return new APIResponse(ResponeCode.SUCCESS, "删除成功");
    }

    @PostMapping("/update")
    @Transactional
    public APIResponse update(ErrorLog errorLog) throws IOException {
        service.updateErrorLog(errorLog);
        return new APIResponse(ResponeCode.SUCCESS, "更新成功");
    }
}
